﻿
#region Copyright & License
// --------------------------
// Copyright 2008 Daniel Gray
// --------------------------
// This file is part of Kale.
//
// Kale is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kale is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Kale.  If not, see <http://www.gnu.org/licenses/>.
#endregion
#region File details
// --------------------------------------------------------
// Filename          : SecurityDispatchMessageInspector.cs
// Author            : Daniel Gray
// Created           : 28/04/2008
// --------------------------------------------------------
#endregion
            
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Web;
using System.Net;

namespace Kale.Web.Services.Behaviours
{
   public class SecurityDispatchMessageInspector : IDispatchMessageInspector
   {
      private static object _loadLock = new object();
      private static bool _uriLoaded;
      private static string _domain;
      private static string _uri;
      private static string _path;

      private string _cookieName;

      public SecurityDispatchMessageInspector(string cookieName)
      {
         _cookieName = cookieName;
      }

      private static void LoadUris(IClientChannel channel)
      {
         lock (_loadLock)
         {
            if (!_uriLoaded)
            {
               Uri uri = channel.LocalAddress.Uri;

               _domain = uri.Host;

               StringBuilder sb = new StringBuilder();

               for (int i = 0; i != uri.Segments.Length - 1; i++)
               {
                  sb.Append(uri.Segments[i]);
               }

               _path = sb.ToString();
               _uri = uri.OriginalString.Replace(uri.AbsolutePath, string.Empty) + _path;
               _uriLoaded = true;
            }
         }
      }

      #region IDispatchMessageInspector Members

      public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
      {
         LoadUris(channel);

         //TODO:Finish this off ;)

         return null;
      }

      public void BeforeSendReply(ref Message reply, object correlationState)
      {
         HttpResponseMessageProperty responseMessageProperty = reply.Properties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;

         CookieContainer container = new CookieContainer();
         container.Add(new Cookie(_cookieName, Guid.NewGuid().ToString(), _path, _domain));
         responseMessageProperty.Headers[HttpResponseHeader.SetCookie] = container.GetCookieHeader(new Uri(_uri));
      }

      #endregion
   }
}
